package cn.edu.scau.cmi.ema.service;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.persistence.Query;
import java.util.Iterator;
@Service("Event4reportService")
@Transactional

public class Event4reportServiceImpl extends CmiEmaService implements Event4reportService {
	public Event4reportServiceImpl() {
	}

	@Transactional
	public Event4report saveEvent4report(Event4report event4report) {

		if (event4report.getId() == null) {

			Event4report existingEvent4report = event4reportDAO
					.findEvent4reportByPhysicalPrimaryKey(event4report.getName());

			if (isEquals(event4report, existingEvent4report)) {
				return existingEvent4report;
			}

			event4report = event4reportDAO.store(event4report);
			event4reportDAO.flush();

		} else {
			Event4report existingEvent4report = event4reportDAO.findEvent4reportByPrimaryKey(event4report.getId());

			event4report = event4reportDAO.store(event4report);
			event4reportDAO.flush();
		}

		return event4report;
	}

	public void deleteEvent4report(Event4report event4report) {
		event4reportDAO.remove(event4report);
		event4reportDAO.flush();
	}

	@Transactional
	public void deleteEvent4reports(Set<Integer> deleteEvent4reportsId) {
		Query query = event4reportDAO.createNamedQuery("deleteEvent4reportsById", -1, -1);
		query.setParameter(1, deleteEvent4reportsId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Event4report> loadEvent4reports() {
		return event4reportDAO.findAllEvent4reports();

	}

	@Transactional
	public Set<Event4report> loadReferenceEvent4reports(Set<Event4report> event4reports) {
		Iterator<Event4report> event4reportsIterator = event4reports.iterator();
		Set<Event4report> referencedEvent4reports = new HashSet<>();
		while (event4reportsIterator.hasNext()) {
			Event4report currentEvent4report = event4reportsIterator.next();

		}
		return referencedEvent4reports;
	}

	public Set<Event4report> loadEvent4reportsByIdSet(Set<Integer> event4reportIdSet) {

		Iterator<Integer> event4reportIdIterator = event4reportIdSet.iterator();
		Set<Event4report> event4reportSet = new HashSet<>();
		while (event4reportIdIterator.hasNext()) {
			Integer id = event4reportIdIterator.next();
			Event4report currentEvent4report = event4reportDAO.findEvent4reportById(id);
			event4reportSet.add(currentEvent4report);
		}
		return event4reportSet;
	}

	@Transactional
	public List<Event4report> findAllEvent4reports(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Event4report>(event4reportDAO.findAllEvent4reports(startResult, maxRows));
	}

	@Transactional
	public Event4report findEvent4reportByPrimaryKey(Integer id) {
		return event4reportDAO.findEvent4reportByPrimaryKey(id);
	}

	@Transactional
	public Integer countEvent4reports() {
		return ((Long) event4reportDAO.createQuerySingleResult("select count(o) from Event4report o").getSingleResult())
				.intValue();
	}

	@Transactional
	public Integer countEvent4reports(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Event4report.class);
		return ((Long) event4reportDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 * 
	 */
	@Transactional
	public Integer countEvent4reportsByName(String queryName) {
		Query query = event4reportDAO.createNamedQuery("countEvent4reportsByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countEvent4reportsByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Event4report.class);
		Session session = entityManager.unwrap(Session.class);
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadEvent4reportObjectReferencedSet(Event4report event4report) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadEvent4reportClassReferencedSet(Event4report event4report) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();

		return classReferencedSetMap;
	}

	public boolean isEquals(Event4report event4report, Object obj) {
		if (obj == event4report)
			return true;
		if (!(obj instanceof Event4report))
			return false;
		Event4report comparedEvent4report = (Event4report) obj;
		if (!CheckPropertyEqual(event4report.getName(), comparedEvent4report.getName())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

